#include "GuiApi.h"
#include "UsefullApi.h"
#include "vmgraph.h"
#include "vmchset.h"
#include "vmstdlib.h"
#include "Lcd.h"
#include "Framework.h"

VMINT GetSoftkeyHeight(void)
{
	switch(GetCurLCDSupport())
	{
	case ENUM_QVGA_SUPPORT:
		return MMI_SOFTKEY_QVGA_HEIGHT;
	}
	return -1;
}

void RefreshScreenByColor(VMBYTE* frame_buf, VMUINT16 color)
{
	vm_graphic_fill_rect(frame_buf, 0, 0, scr_width, scr_height, color, color);
}

void DrawImage(VMBYTE* frame_buf, VMSTR name, VMINT x, VMINT y, VMINT width, VMINT height)
{
	VMUINT8* pData = get_img_by_name(name);
	
	if(pData)
	{
		//vm_graphic_set_clip(x, y, x + width, y + height);
		vm_graphic_blt(frame_buf, x, y, pData, 0, 0, width, height, 1);
	}
	free_img(pData);
}

void DrawImageDirect(VMBYTE* frame_buf, VMUINT8* img, VMINT x, VMINT y, VMINT width, VMINT height)
{
	if(img)
	{
		//vm_graphic_set_clip(x, y, x + width, y + height);
		vm_graphic_blt(frame_buf, x, y, img, 0, 0, width, height, 1);
	}
}

void DrawText(VMBYTE* frame_buf, VMSTR text, VMINT x, VMINT y, VMUINT16 color)
{
	VMSTR pSrc;
	VMINT sizeSrc;
	VMINT malloc_len;
	VMWSTR pDst;
	
	if(text == NULL)
	{
		return;
	}
	
	pSrc = text;
	sizeSrc = strlen(pSrc);
	malloc_len = (sizeSrc+1)*sizeof(VMWCHAR);
	pDst = (VMWSTR)vm_malloc(malloc_len);
	memset(pDst, malloc_len, 0);
	
	vm_ascii_to_ucs2(pDst, malloc_len, pSrc);
	vm_graphic_textout(frame_buf, x, y, pDst, sizeSrc, color);
	vm_free(pDst);
}

void DrawTextUCS2(VMBYTE* frame_buf, VMWSTR text, VMINT x, VMINT y, VMUINT16 color)
{
	VMINT sizeSrc;
	
	if(text == NULL)
	{
		return;
	}
	
	sizeSrc = vm_wstrlen(text);
	vm_graphic_textout(frame_buf, x, y, text, sizeSrc, color);
}

void DrawTextAlignment(VMBYTE* frame_buf, VMSTR text, VMINT x, VMINT y, VMINT width, VMUINT16 color, VMINT alignment)
{
	VMWSTR pDst;
	VMINT str_width;
	VMINT x0, y0;
	
	if(text == NULL)
	{
		return;
	}
	
	pDst = ascii_to_ucs2(text);
	str_width = vm_graphic_get_string_width(pDst);
	
	switch(alignment)
	{
	case ALIGN_LEFT_MODE:
		x0 = x;
		y0 = y;
		DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		break;
	case ALIGN_CENTER_MODE:
		if(str_width >= width)
		{
			x0 = x;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		else
		{
			x0 = x + (width - str_width)/2;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		break;
	case ALIGN_RIGHT_MODE:
		if(str_width >= width)
		{
			x0 = x;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		else
		{
			x0 = x + (width - str_width);
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		break;
	}

	free_str(pDst);
}

void DrawTextUCS2Alignment(VMBYTE* frame_buf, VMWSTR text, VMINT x, VMINT y, VMINT width, VMUINT16 color, VMINT alignment)
{
	VMWSTR pDst;
	VMINT str_width;
	VMINT x0, y0;
	
	if(text == NULL)
	{
		return;
	}
	
	pDst = text;
	str_width = vm_graphic_get_string_width(pDst);
	
	switch(alignment)
	{
	case ALIGN_LEFT_MODE:
		x0 = x;
		y0 = y;
		DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		break;
	case ALIGN_CENTER_MODE:
		if(str_width >= width)
		{
			x0 = x;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		else
		{
			x0 = x + (width - str_width)/2;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		break;
	case ALIGN_RIGHT_MODE:
		if(str_width >= width)
		{
			x0 = x;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		else
		{
			x0 = width - str_width;
			y0 = y;
			DrawTextUCS2(frame_buf, pDst, x0, y0, color);
		}
		break;
	}
}

void DrawRoundRect(VMBYTE* frame_buf, VMINT x, VMINT y, VMINT width, VMINT height, VMINT corner_width, VMINT border_width, VMUINT16 border_color, VMINT bg_color)
{
	VMINT i;
	vm_graphic_fill_roundrect(frame_buf, x + border_width, y + border_width, width - border_width*2, height - border_width*2, corner_width, bg_color);
	for(i = border_width - 1;i >= 0;i--)
	{
		vm_graphic_roundrect(frame_buf, x + i, y + i, width - i*2, height - i*2, corner_width, border_color);
	}
}

void DrawFillRect(VMBYTE* frame_buf, VMINT x, VMINT y, VMINT width, VMINT height, VMUINT16 line, VMUINT16 back)
{
	vm_graphic_fill_rect(frame_buf, x, y, width, height, line, back);
}
